package org.robert.export_client.excel.builder;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.robert.export_client.excel.entity.CellStyleEntity;
import org.robert.export_client.excel.entity.WorkbookRespWrite;
import org.robert.export_client.excel.metadata.AbstractSheetBuilder;
import org.robert.export_client.excel.metadata.AbstractWorkbookBuilder;
import org.robert.export_client.excel.metadata.SourceSheetBuilder;
import org.robert.export_client.excel.util.ExcelUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * ExcelWorkbookBuilder:
 *
 * @author zhaoshengtao
 * @date 2024/01/19
 */
public class WorkbookDataRespBuilder extends AbstractWorkbookBuilder {

    public WorkbookDataRespBuilder(HSSFWorkbook workbook, HttpServletResponse response,
                                   OutputStream outputStream, String fileName) throws UnsupportedEncodingException {
        super(outputStream, response, workbook, fileName);
    }

    public static WorkbookDataRespBuilder create(HttpServletResponse response) throws IOException {
        return create(response, null);
    }

    public static WorkbookDataRespBuilder create(HttpServletResponse response, String filename) throws IOException {
        return new WorkbookDataRespBuilder(ExcelUtil.createWorkBook(), response, response.getOutputStream(), filename);
    }

    public AbstractSheetBuilder createSheet(String sheetName, Class clazz) throws Exception {

        return createSheet(sheetName, clazz, null);
    }

    @Override
    public ExcelSheetDataRespBuilder createSheet(String sheetName, Class<?> clazz,
                                            Map<String, Map<String, String>> dictMap) throws Exception {
        if(StringUtils.isBlank(sheetName)){
            sheetName = "sheet1";
        }
        HSSFSheet sheet = ExcelUtil.createSheet(this.getWorkbook(), sheetName);
        return new ExcelSheetDataRespBuilder(this, sheet, clazz, dictMap);
    }

    @Override
    public AbstractSheetBuilder createSheet(String sheetName, Class<?> clazz, Map<String, Map<String, String>> dictMap, CellStyleEntity titleStyle) throws Exception {
        if(StringUtils.isBlank(sheetName)){
            sheetName = "sheet1";
        }
        HSSFSheet sheet = ExcelUtil.createSheet(this.getWorkbook(), sheetName);
        return new ExcelSheetDataRespBuilder(this, sheet, clazz, dictMap, titleStyle);
    }

    @Override
    public SourceSheetBuilder createSheet(String sheetName, Class<?> clazz, Map<String, Map<String, String>> dictMap, Integer limit) throws Exception {
        return null;
    }

    @Override
    public SourceSheetBuilder createSheet(String sheetName, Class<?> clazz, Map<String, Map<String, String>> dictMap, Integer limit, CellStyleEntity titleStyle) throws Exception {
        return null;
    }

    @Override
    public WorkbookRespWrite build(){
        return new WorkbookRespWrite(this);
    }
}
